/**
 * 
 */
package com.lijin.workshop.dao.jdbc;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ObjectUtils;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

/**
 * @author jin
 *
 */
public class MagicDAO extends JdbcDaoSupport {
	
	public void getEmployeesByPage(Map<String, Object> qryParams, int pageNo, int pageSize) {
		
	}

	public String getEmployees() {
		final List<String> qryLst = new ArrayList<String>();
		this.getJdbcTemplate().query("select * from t_employee limit 0, 1", new RowCallbackHandler() {
			@Override
			public void processRow(ResultSet rs) throws SQLException {
				ResultSetMetaData meta = rs.getMetaData();
				StringBuilder sb = new StringBuilder();
				sb.append("{");
				for (int i = 1; i <= meta.getColumnCount(); i ++) {
					sb.append("\"");
					sb.append(meta.getColumnName(i));
					sb.append("\":\"");
					sb.append(ObjectUtils.toString(rs.getObject(i)));
					sb.append("\"");
					if (i != meta.getColumnCount()) {
						sb.append(",");
					}
				}
				sb.append("}");
				qryLst.add(sb.toString());
			}
		});
		
		StringBuilder rstStr = new StringBuilder();
		if (qryLst.size() > 0) {
			rstStr.append("{\"rows\":[");
			for (int i = 0; i < qryLst.size(); i ++) {
				rstStr.append(qryLst.get(i));
				if (i < qryLst.size() - 1) {
					rstStr.append(",");
				}
			}
			rstStr.append("]}");
		}
		return rstStr.toString();
	}
	
}